package com.carlinfo.userscenter.stu.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.carlinfo.common.dao.impl.BaseDaoImpl;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.userscenter.stu.dao.IAnswerDao;
import com.carlinfo.userscenter.stu.pojo.Answer;

public class answerDaoImpl extends BaseDaoImpl<Answer> implements IAnswerDao
{
	public answerDaoImpl()
	{
		Answer answer = new Answer();
		this.baseObj = answer;
	}
	@Override
	public int saveOneDao(Answer answer)
	{
		String sql = "insert into answer values(null,?,?,?,?,?,?,?,?,?)";
		List<Object> list = new ArrayList<Object>();
		list.add(answer.getStuId());
		list.add(answer.getQuestionId());
		list.add(answer.getShiFou());
		list.add(answer.getJieGuo());
		list.add(answer.getCreateTime());
		list.add(answer.getEndTime());
		list.add(answer.getPubTime());
		list.add(answer.getStatus());
		list.add(answer.getContext());
		
		
		Map<String, Object> condMap = new HashMap<String, Object>();

		int res = this.excJDBC(sql, list, condMap);
		return res;
	}

	@Override
	public List<Answer> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<Object> list = new ArrayList<Object>();
		String sql = "select * from answer where 1=1 ";
		/*
		 * 关键字: select * from a_dynasty where (name like '%0%' or capital like '%0%' or
		 * context like '%0%'); 状态 : select * from a_dynasty where status = 1 ; 时间 :
		 * select * from a_dynasty where (pubTime >= '2018-09-18 00:00:00' and pubTime <
		 * '2018-09-19 10:00:00'); 关键字+状态: select * from a_dynasty where (name like
		 * '%0%' or capital like '%0%' or context like '%0%') and status = 1 ; 关键字+时间:
		 * select * from a_dynasty where (name like '%0%' or capital like '%0%' or
		 * context like '%0%') and (pubTime >= '2018-09-18 00:00:00' and pubTime <
		 * '2018-09-19 10:00:00'); 时间+状态: select * from a_dynasty where status = 1 and
		 * (pubTime >= '2018-09-18 00:00:00' and pubTime < '2018-09-19 10:00:00');
		 * 关键字+状态+时间: select * from a_dynasty where (name like '%0%' or capital like
		 * '%0%' or context like '%0%') and status = 1 and (pubTime >= '2018-09-18
		 * 00:00:00' and pubTime < '2018-09-19 10:00:00'); 啥也不选: select * from
		 * a_dysnasty ;
		 */

		/* 根据条件来组装sql语句 */
		if (condMap.get("keyWord") != null && !"".equalsIgnoreCase(condMap.get("keyWord") + ""))
		{
			sql += " and (name like ? or jieGuo like ? or context like ?) ";
			list.add("%" + condMap.get("keyWord") + "%");
			list.add("%" + condMap.get("keyWord") + "%");
			list.add("%" + condMap.get("keyWord") + "%");
		}
		if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sql += " and status = ? ";
			list.add(condMap.get("status"));
		}
		if (condMap.get("stDate") instanceof Date && condMap.get("edDate") instanceof Date)
		{
			sql += " and  (pubTime >= ? and pubTime < ?) ";
			list.add(condMap.get("stDate"));
			list.add(condMap.get("edDate"));
		}

		/**
		 * 设置排序方式
		 */
		if ("idAsc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sql += " order by id asc ";
		} else if ("idAscNameDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sql += " order by id asc,name desc ";
		} else
		{
			sql += " order by pubTime desc ";
		}

		if (pageInfoUtil != null)
		{
			return this.findListByPage(pageInfoUtil, sql, list);
		}
		return this.findList(sql, list);
	}

	/**
	 * 将数据库转换为pojo
	 * 
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
//	protected Students convertFromDb_source(ResultSet rs) throws SQLException
//	{
//		Students dynasty = new Students();
//		dynasty.setAge(rs.getInt("age"));
//		dynasty.setCapital(rs.getString("capital"));
//		dynasty.setId(rs.getInt("id"));
//		dynasty.setContext(rs.getString("context"));
//		dynasty.setCreateTime(rs.getTimestamp("createTime"));
//		dynasty.setName(rs.getString("name"));
//		dynasty.setPubTime(rs.getTimestamp("pubTime"));
//		dynasty.setStatus(rs.getByte("status"));
//		dynasty.setUpdateTime(rs.getTimestamp("updateTime"));
//		dynasty.setSt(rs.getTimestamp("st"));
//		return dynasty;
//	}

//	@Override
//	public Answer selectOneDao(Map<String, Object> condMap)
//	{
//		
//	}

	@Override
	public int deleteOneDao(Map<String, Object> condMap)
	{
		List<Object> list = new ArrayList<Object>();
		String sql = "delete from answer ";
		if (!"".equalsIgnoreCase((condMap.get("id") + "")) && condMap.get("id") != null)
		{
			sql += "where id = ?;";
			list.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase((condMap.get("name") + "")) && condMap.get("name") != null)
		{
			sql += "where name = ? ;";
			list.add(condMap.get("name"));
		}
		return this.excJDBC(sql, list);
	}
	@Override
	public Answer selectOneDao(Map<String, Object> condMap)
	{
		List<Object> list = new ArrayList<Object>();
		String sql = "select * from answer ";
		if (!"".equalsIgnoreCase((condMap.get("id") + "")) && condMap.get("id") != null)
		{
			sql += "where id = ? limit 1;";
			list.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase((condMap.get("name") + "")) && condMap.get("name") != null)
		{
			sql += "where name = ? limit 1;";
			list.add(condMap.get("name"));
		}
		return this.findONeJDBC(sql, list);
	}
	
	@Override
	public int updateOneDao(Answer answer)
	{
		String sql = "update answer set stuId=?,questionId=?,shiFou=?,jieGuo=?"
				+ ",createTime=?,endTime=? ,pubTime=?,status=?,context=? where id = ?";
		List<Object> list = new ArrayList<Object>();
		list.add(answer.getStuId());
		list.add(answer.getQuestionId());
		list.add(answer.getShiFou());
		list.add(answer.getJieGuo());
		list.add(answer.getCreateTime());
		list.add(answer.getEndTime());
		list.add(answer.getPubTime());
		list.add(answer.getStatus());
		list.add(answer.getContext());
		list.add(answer.getId());
		return this.excJDBC(sql, list);
	}
	@Override
	public int selectMaxId()
	{
		// TODO Auto-generated method stub
		return 0;
	}

}
